/**
 * Created by admin on 2016/2/20.
 */
var grApp = angular.module('gr-app', ['toaster', 'ngAnimate']);

grApp.controller('LanternResult',['$scope','$http','BaseService','$timeout',function($scope,$http,BaseService,$timeout){
    $scope.card = {} ;
    $scope.cardStatus = {
        'lan1':true,
        'lan2':true,
        'lan3':true,
        'lan4':true,
        'lan5':true,
        'lan6':true,
    }
    $scope.reward={jiangpin:false,img:'/images/activity/lantern/jiangping1.png',advertisement:'果然100恭祝猴年大吉'}
    $scope.isFirst = {first:false };
    $scope.load = function(data,isFirst){
        console.log(data)
        $scope.card = data ;
        $scope.cardStatus = {
            'lan1':data.lan1.number===0,
            'lan2':data.lan2.number===0,
            'lan3':data.lan3.number===0,
            'lan4':data.lan4.number===0,
            'lan5':data.lan5.number===0,
            'lan6':data.lan6.number===0,
        };
        //<%=data.isFirst%>
        if(isFirst){
            $scope.isFirst.first = true ;
        }
        $scope.aaa = true ;
        console.log($scope.cardStatus);
    };
    $scope.shake = false;
    $scope.playlao = function(){
        BaseService.getCurrentReward().then(function(data){
            if(data.code === 0){
                console.log(data.rewardPool)
                if(data.rewardPool.type ===2){
                    //需要使用积分
                    if(!confirm('将要扣除20积分，是否继续？')){
                        return ;
                    }
                }
                $scope.shake = true;
                $timeout(function(){
                    $scope.shake = false;
                    $scope.laoLantern();
                },1000);
            }else{
                $scope.shake = true;
                $timeout(function(){
                    $scope.shake = false;
                },1000);
            }
        }).catch(function(){
            $scope.shake = true;
            $timeout(function(){
                $scope.shake = false;
            },1000);
        });
    };
    $scope.laoLantern = function(){
        BaseService.give_reward().then(function(res){
            if(res.code <= -1){
                alert(res.msg)
                return ;
            }
            var data = res.result;
            var card = res.card ;
            if(data.code === 1){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping1.png';
            }
            if(data.code === 2){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping2.png';
            }
            if(data.code === 3){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping3.png';
            }
            if(data.code === 4){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping4.png';
            }
            if(data.code === 5){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping5.png';
            }
            if(data.code === 6){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping6.png';
            }
            if(data.code === 7){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping7.png';
            }
            if(data.code === 8){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping8.png';
            }
            if(data.code === 9){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping9.png';
            }
            if( data.code >= 10 && data.code!=9999){
                $scope.reward.jiangpin = true;
                $scope.reward.img = '/images/activity/lantern/jiangping9.png';
                //显示广告
                /**
                 * TODO:广告内容存放位置？？？？
                 * @type {*|number|{jiangpin: boolean, img: string, advertisement: string}|string}
                 */
                $scope.reward.advertisement = data.reward;
            }else{
                $scope.reward.advertisement = "果然100恭祝猴年大吉" ;
            }

            if([1,2,3,4,5,6].indexOf(data.code)>=0){
                $scope.card = {} ;
                card = JSON.parse(card)
                $scope.card = card;
                $scope.cardStatus = {
                    'lan1':card.lan1.number===0,
                    'lan2':card.lan2.number===0,
                    'lan3':card.lan3.number===0,
                    'lan4':card.lan4.number===0,
                    'lan5':card.lan5.number===0,
                    'lan6':card.lan6.number===0,
                };
            }

        }).catch(function(err){
            //alert("出错了")
        });
    };

    $scope.get3CardRandom = function(){
        BaseService.get3CardRandom().then(function(data){
            $scope.isFirst.first = false ;
            if(data.code === 0){
                console.log(JSON.parse(data.card));
                $scope.card = {} ;
                $scope.card = JSON.parse(data.card);
                $scope.cardStatus = {
                    'lan1':JSON.parse(data.card).lan1.number===0,
                    'lan2':JSON.parse(data.card).lan2.number===0,
                    'lan3':JSON.parse(data.card).lan3.number===0,
                    'lan4':JSON.parse(data.card).lan4.number===0,
                    'lan5':JSON.parse(data.card).lan5.number===0,
                    'lan6':JSON.parse(data.card).lan6.number===0,
                };
                //alert('赠送成功');
            }else{
                //alert(data.msg);
            }
        });
    }
    $scope.alertM = function(){
        //ng-class="{true:'animated slideInDown'}[reward.jiangpin]"
        console.log($scope.reward.jiangpin);
        $scope.reward.jiangpin = true;
        console.log($scope.reward.jiangpin);
    };
    $scope.hidelaoqudivBtn = function(){
        $scope.reward.jiangpin = false;
    }

    $scope.seekCard = function(event,reward){
        alert(123);
    }
    $scope.giveCard = function(event,reward){
        BaseService.give_card(reward) ;
    }


    $scope.giveCardToFriend = function(){
        alert(123);
        //href="/extra/lantern/yao_get/<%=card_id%>"
    }

}]);
grApp.controller('FromYao',['$scope','$http','BaseService','$timeout',function($scope,$http,BaseService,$timeout){
    $scope.giveToFriend = function(reward,reward_name,ownuid){
        BaseService.give_to_friend(reward,reward_name,ownuid).then(function(data){
            if(data.code === 0){
                alert('赏赐成功');
                window.location.reload();
            }else{
                alert("您的元宵数量不足");
                window.location.href('/extra/lantern/index');
            }
        }).catch(function(err){
            alert("您的元宵数量不足");
            window.location.href('/extra/lantern/index');
        });
    }
}]);
grApp.controller('FromSong',['$scope','$http','BaseService','$timeout',function($scope,$http,BaseService,$timeout){
    $scope.getFromFriend = function(reward,reward_name,ownuid,card_id){
        BaseService.getFromFriend(reward,reward_name,ownuid,card_id).then(function(data){
            if(data.code === 0){
                alert('领取成功');
                window.location.reload();
            }else{
                alert(data.msg)
                window.location.reload();
            }
        }).catch(function(err){
            window.location.reload();
        });
    }
}]);
grApp.controller('FinalReward',['$scope','$http','BaseService','$timeout',function($scope,$http,BaseService,$timeout){

    $scope.wx_inited = false;
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }else{
        onBridgeReady();
    }
    function onBridgeReady(){
        $scope.wx_inited = true;
        //alert('ok');
        //todo some wx things
    }



    $scope.card = {} ;
    $scope.cardStatus = {
        'lan1':true,
        'lan2':true,
        'lan3':true,
        'lan4':true,
        'lan5':true,
        'lan6':true,
    };
    $scope.btnStatus ={
        give_apple:false,
        give_amount:false,
        give_coupon:false,
        chongzhi:false
    }
    $scope.isFirst = {first:false };
    $scope.load = function(data,isFirst){
        $scope.card = data ;
        $scope.cardStatus = {
            'lan1':data.lan1.number===0,
            'lan2':data.lan2.number===0,
            'lan3':data.lan3.number===0,
            'lan4':data.lan4.number===0,
            'lan5':data.lan5.number===0,
            'lan6':data.lan6.number===0,
        };
        BaseService.get_zsm_info().then(function(data){
            console.log(data);
            if(data.code === 0){
                var info = data.zsmInfo ;
                if(info.cnt === 5 && !info.w_flag) {
                    $scope.btnStatus.give_apple = true;
                }
                if(info.cnt === 6 && !info.l_flag) {
                    $scope.btnStatus.give_amount = true;
                }
                if(info.size >= 50 && info.size < 100 && !info.y_flag) {
                    $scope.btnStatus.give_coupon = true;
                }
                if(info.size >= 100 && !info.c_flag) {
                    $scope.btnStatus.chongzhi = true;
                }
            }
        })
    };
    $scope.give_apple = function(){
        BaseService.give_apple().then(function(data){
            /*console.log(data)
            $scope.btnStatus.give_apple = false;*/
            if(data.code === 0){
                alert('领取成功,请前往个人中心查看');
            }else{
                alert(data.msg);
            }
        }).catch(function(err){
            $scope.btnStatus.give_apple = false;
            alert(err.msg);
        })
    };
    $scope.give_amount = function(){
        BaseService.give_amount().then(function(data){
            if(data.code === 0){
                alert('领取成功,请前往个人中心查看');
            }else{
                alert(data.msg);
            }
        }).catch(function(err){
            alert(err.msg);
        })
    };
    $scope.give_coupon = function(){
        BaseService.give_coupon().then(function(data){
            if(data.code === 0){
                alert('领取成功,请前往个人中心查看');
            }else{
                alert(data.msg);
            }
        }).catch(function(err){
            alert(err.msg);
        })
    };

    $scope.doRecharge = function(){
        var timestamp = new Date().getTime();
        var orderid = 'YX'+timestamp;
        $http({
            methd:'GET',
            url:'/user/recharge/'+orderid+'/120',
        }).success(function(datas,status,headers,config){
            if(datas.code!=0){
                alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
                return;
            }
            var fee=12000;
            if($scope.wx_inited){
                BaseService.wxpay(orderid,fee).then(function(data){
                    //更新状态为1500
                    //todo...
                    alert("噢耶~充值成功!快去个人中心看看吧~");
                    window.location.href='/user/center';
                }).catch(function(err){
                    alert(">_< ~ OMG! 就这么残忍的拒绝我了么?");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                //window.location.href='/extra/lantern/index1';
            }
        }).error(function(data, status, headers, config){
            alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
        });

    };




}]);

grApp.service('BaseService',['$http','$q',function($http,$q) {
    var service = {};
    service.give_reward = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/give_reward'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.wxpay = function(trade_no,total_fee){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http.post('/wx/pay', {trade_no:trade_no,total_fee:total_fee}).
            success(function(data, status, headers, config) {
                WeixinJSBridge.invoke('getBrandWCPayRequest',data,
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                            deferred.resolve(res);
                        }else{
                            deferred.reject(res);
                        }
                    }
                );
            }).
            error(function(data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    service.give_card = function (card_id) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/give/'+card_id,
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.give_to_friend = function (reward,reward_name,ownuid) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/yao_get',
            params:{reward:reward,reward_name:reward_name,ownuid:ownuid}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getFromFriend = function (reward,reward_name,ownuid,card_id) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/song_get',
            params:{reward:reward,reward_name:reward_name,ownuid:ownuid,card_id:card_id}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.get3CardRandom = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/get3CardRandom'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getCurrentReward = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/getCurrentReward'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.get_zsm_info = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/get_zsm_info'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.give_apple = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/give_apple'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.give_amount = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/give_amount'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.give_coupon = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/extra/lantern/give_coupon'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    return service;
}]);